#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextDocument>
#include <QTextBlock>
#include <QRegExp>
#include <QDebug>
#include <QClipboard>
#include <QMessageBox>
#include "file_base.h"


const QString MainWindow::C_UCOZ_LINK = "http://straypetersburg.ucoz.ru/";
const QString MainWindow::C_NAROD_LINK = "http://stray-petersburg.narod.ru/";

//-----------------------------------------------------------------------------
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->edDateRazm->setDateTime(QDateTime::currentDateTime());

    int m_cur_ind = QDateTime::currentDateTime().date().month()-1;
    ui->cb_m_rus->setCurrentIndex(m_cur_ind);
    ui->cb_m_fin->setCurrentIndex(m_cur_ind);
    ui->cb_m_eng->setCurrentIndex(m_cur_ind);

    cfg_file = new QConfigFile(".stray-petersburg", "forum_text");
    cfg_file->ReadFile();
    QVariant id;
    cfg_file->GetParam("ID",id);
    ui->edID->setText(id.toString());

    on_edMaleFemale_currentIndexChanged(ui->edMaleFemale->currentIndex());

    QVariant rdImgFold;
    cfg_file->GetParam("RbImgFolder", rdImgFold);

    if(rdImgFold=="narod")
        ui->rd_ImgPathNarod->setChecked(true);
    else
        ui->rd_ImgPathUcoz->setChecked(true);

    cfg_file->GetParam("UcozPhotoPath", rdImgFold);
    ui->ed_UcozRootFold->setText(rdImgFold.toString());

    GenerateRezPath();

}
//-----------------------------------------------------------------------------
MainWindow::~MainWindow()
{
    cfg_file->SetParam("ID",ui->edID->text());
    cfg_file->WriteFile();
    delete cfg_file;
    delete ui;
}
//-----------------------------------------------------------------------------
QString MainWindow::ReplaceUml(const QString& in_str)
{
    QString rez = in_str.trimmed();

    rez = rez.replace(QString(trUtf8("&")), QString("&#38;"));
    rez = rez.replace(QString(trUtf8("<")), QString("&#60;"));
    rez = rez.replace(QString(trUtf8(">")), QString("&#62;"));

    rez = rez.replace(QString(trUtf8("Ö")), QString("&Ouml;"));
    rez = rez.replace(QString(trUtf8("ö")), QString("&ouml;"));
    rez = rez.replace(QString(trUtf8("Ä")), QString("&Auml;"));
    rez = rez.replace(QString(trUtf8("ä")), QString("&auml;"));

    rez = rez.replace(QString(trUtf8("Ž")), QString("&#381;"));
    rez = rez.replace(QString(trUtf8("ž")), QString("&#382;"));

    rez = rez.replace(QString(trUtf8("Š")), QString("&#352;"));
    rez = rez.replace(QString(trUtf8("š")), QString("&#353;"));

    rez = rez.replace(QString(trUtf8("Ý")), QString("&Yacute;"));
    rez = rez.replace(QString(trUtf8("ý")), QString("&yacute;"));

    return rez;
}

//-----------------------------------------------------------------------------
QString MainWindow::ReplaceUml_file(const QString& in_str)
{
    QString rez = in_str.trimmed();

    rez = rez.replace(QString(trUtf8("Ö")), QString("O"));
    rez = rez.replace(QString(trUtf8("ö")), QString("o"));
    rez = rez.replace(QString(trUtf8("Ä")), QString("A"));
    rez = rez.replace(QString(trUtf8("ä")), QString("a"));

    rez = rez.replace(QString(trUtf8("Ž")), QString("Z"));
    rez = rez.replace(QString(trUtf8("ž")), QString("z"));

    rez = rez.replace(QString(trUtf8("Š")), QString("S"));
    rez = rez.replace(QString(trUtf8("š")), QString("s"));

    rez = rez.replace(QString(trUtf8("Ý")), QString("Y"));
    rez = rez.replace(QString(trUtf8("ý")), QString("y"));

    rez = rez.replace(QString(" "), QString("_"));
    rez = rez.replace(QString("("), QString("_"));
    rez = rez.replace(QString(")"), QString("_"));
    rez = rez.replace(QString("&"), QString("_"));

    return rez;
}

//-----------------------------------------------------------------------------
QString MainWindow::CheckGram(const QString& in_str)
{
    QString rez = in_str;

    // Убираем пробелы перед знаками припинания
    rez = rez.replace(QString(" ,"), QString(","));
    rez = rez.replace(QString(" ."), QString("."));
    rez = rez.replace(QString(" ?"), QString("?"));
    rez = rez.replace(QString(" !"), QString("!"));
    rez = rez.replace(QString(" :"), QString(":"));
    rez = rez.replace(QString(" ;"), QString(";"));

    rez = rez.replace(QRegExp("\\s+"), " ");

    rez = rez.replace(QRegExp("\\.([^\\d\\.\\s])"), ". \\1");
    rez = rez.replace(QRegExp("(\\D)\\.([^\\.\\s])"), "\\1. \\2");

    rez = rez.replace(QRegExp("\\,([^\\,\\s])"), ", \\1");

    rez = rez.replace(QRegExp("\\?([^\\!\\?\\s])"), "? \\1");
    rez = rez.replace(QRegExp("\\!([^\\!\\?\\s])"), "! \\1");

    rez = rez.replace(QRegExp("(\\s)\\-([^\\-\\s])"), "\\1- \\2");
    rez = rez.replace(QRegExp("(\\S)\\-(\\s)"), "\\1 -\\2");

    rez = rez.replace(QRegExp("([^\\s\\:\\;\\()])\\("), "\\1 (");
    rez = rez.replace(QRegExp("\\(\\s"), "(");

    rez = rez.replace(QRegExp("\\s\\)"), ")");
    rez = rez.replace(QRegExp("\\)([^\\s\\)])"), ") \\1");

    return rez;
}
//-----------------------------------------------------------------------------
QString MainWindow::ProcessString(const QString& in_str, bool repl_uml)
{
    QString rez = in_str;

    rez = CheckGram(rez);

    if(repl_uml)
        rez = ReplaceUml(rez);

    rez += "<br />\n";
    return rez;

}
//-----------------------------------------------------------------------------
// только RUS и FIN
QString MainWindow::CreateNarodCapt(QNational nation)
{
    QString rez = "";
    bool is_rus = (nation==RUS);
    rez += "<!-- Zapis " + ui->edID->text() + " -->\n";

    rez+= "<div class=\"PesName\">";
    rez+= ui->edID->text()+". ";
    if(is_rus) rez+= ReplaceUml(ui->edNameRus->text());
    else       rez+= ReplaceUml(ui->edNameFin->text());
    rez+="</div>\n";

    rez+= "<div class=\"p_date\">(";
    if(is_rus) rez+= trUtf8("Дата размещения: ");
    else    rez+= "P&auml;iv&auml;ys ad: ";
    rez+= ui->edDateRazm->text()+")</div>\n";

    rez+= "<div class=\"p_right\">";
    if(is_rus) rez+= trUtf8("Дата рождения: ");
    else    rez+= "Arvioitu syntym&auml;aika: ";
    rez+= ui->edDateRojd->text()+"</div>\n";


    rez+= "<div class=\"p_right\">";
    if(is_rus) rez+= trUtf8("Рост в холке: ");
    else    rez+= "S&auml;k&auml;korkeus: ";
    rez+= ui->edRost->text()+" cm</div>\n";

    rez+= "<div class=\"p_right\">";
    if(is_rus) rez+= trUtf8("Вес: ");
    else    rez+= "Paino: ";
    rez+= ui->edVes->text()+" kg</div>\n";

    rez+= "<p> </p>\n";
    return rez;
}

//-----------------------------------------------------------------------------
// Фотки для сайта KK
QString MainWindow::AddImgForKK_OneImg(int imgNum)
{
    return QFileBase::ConcatDirFile(GetPhotoPathForKK(), GeneratePhotoFileName(imgNum));
}
//-----------------------------------------------------------------------------
QString MainWindow::CreateKKCapt()
{
    QString mt = "0";
    QString mb = "0";
    QString ml = "0";
    QString mr = "10";
    QString av_w = "\" width=\"150\" /></div>\n";

    switch(ui->cb_Avatar->currentIndex()){
        case 0:// широкий
            mt = "20";
            mb = "20";
            ml = "0";
            mr = "10";
            break;
        case 1:// высокий
            mt = "0";
            mb = "0";
            ml = "15";
            mr = "15";
            av_w = "\" height=\"150\" /></div>\n";
            break;
    }

    QString rez = "<div><img style=\"margin-top: " + mt +
            "px; margin-right: " + mr + "px; margin-bottom: " + mb +
            "px; margin-left: " + ml + "px; float: left;\" src=\"";

    rez+=AddImgForKK_OneImg(1);

    rez += av_w;

    rez+= "<p><strong>id N"+ ui->edID->text() + "</strong></p>\n";
    rez+= trUtf8("<p><strong>Arvioitu syntymäaika: ") + ui->edDateRojd->text() + "</strong></p>\n";
    rez+= trUtf8("<p><strong>Säkäkorkeus: ") + ui->edRost->text() + " cm</strong></p>\n";
    rez+= "<p><strong>Paino: " + ui->edVes->text() + " kg</strong></p>\n";
    rez+= "<p><strong>Pietarissa</strong></p>\n";
    rez+= "<hr id=\"system-readmore\" />\n";
    return rez;
}
//-----------------------------------------------------------------------------
QString MainWindow::Create_1_DopLinks(const QString& cur_txt, const QString& link1, const QString& text1)
{
    QString rez("");

    if(link1.length()>0){
        if(cur_txt.length()>0) rez += " ";
        rez += "<a href=\"" + link1 + "\" target=\"_blank\">";
        if(text1.length()>0) rez += text1;
        else                 rez += link1;

        rez += "</a>";
    }
    return rez;
}
//-----------------------------------------------------------------------------
QString MainWindow::CreateDopLinksOneNation(const QString& text_beg,
                                   const QString& text_l1,
                                   const QString& text_l2,
                                   const QString& text_l3,
                                   const QString& text_l4,
                                   const QString& text_l5,
                                   const QString& text_end,
                                   bool  first_p)
{
    QString rez("");

    if(text_beg.length()>0)
        rez += text_beg;

    rez += Create_1_DopLinks(rez, ui->doplnk_1->text(), text_l1);
    rez += Create_1_DopLinks(rez, ui->doplnk_2->text(), text_l2);
    rez += Create_1_DopLinks(rez, ui->doplnk_3->text(), text_l3);
    rez += Create_1_DopLinks(rez, ui->doplnk_4->text(), text_l4);
    rez += Create_1_DopLinks(rez, ui->doplnk_5->text(), text_l5);

    if(text_end.length()>0){
        if(rez.length()>0) rez += " ";
        rez += text_end;
    }

    if(rez.length()>0) rez = ((first_p)?"<p>":"") + rez + "</p>\n";

    return rez;
}

//-----------------------------------------------------------------------------
QString MainWindow::CreateDopLinks(QNational nation,
                                   bool first_p)// первый <p>)
{
    switch(nation){
        case ENG:
        case RUS: return CreateDopLinksOneNation(ReplaceUml(ui->doplnk_text_begin_r->text()),
                       ReplaceUml(ui->doplnk_1_r->text()),
                       ReplaceUml(ui->doplnk_2_r->text()),
                       ReplaceUml(ui->doplnk_3_r->text()),
                       ReplaceUml(ui->doplnk_4_r->text()),
                       ReplaceUml(ui->doplnk_5_r->text()),
                       ReplaceUml(ui->doplnk_text_end_r->text()),first_p);

        case FIN: return CreateDopLinksOneNation(ReplaceUml(ui->doplnk_text_begin_f->text()),
                       ReplaceUml(ui->doplnk_1_f->text()),
                       ReplaceUml(ui->doplnk_2_f->text()),
                       ReplaceUml(ui->doplnk_3_f->text()),
                       ReplaceUml(ui->doplnk_4_f->text()),
                       ReplaceUml(ui->doplnk_5_f->text()),
                       ReplaceUml(ui->doplnk_text_end_f->text()),first_p);
        case SWE: return CreateDopLinksOneNation(ReplaceUml(ui->doplnk_text_begin_s->text()),
                       ReplaceUml(ui->doplnk_1_s->text()),
                       ReplaceUml(ui->doplnk_2_s->text()),
                       ReplaceUml(ui->doplnk_3_s->text()),
                       ReplaceUml(ui->doplnk_4_s->text()),
                       ReplaceUml(ui->doplnk_5_s->text()),
                       ReplaceUml(ui->doplnk_text_end_s->text()),first_p);
    }
    return "";
}

//-----------------------------------------------------------------------------
QString MainWindow::ProcessText(QPlainTextEdit *edit_in, bool repl_uml, bool first_p)
{
    QString rez("");

    if(first_p)
        rez += "<p>";

    QTextDocument *doc = edit_in->document();
    int	bl_cnt = doc->blockCount ();
    for(int b=0; b<bl_cnt; b++){
        QTextBlock bl_i = doc->findBlockByNumber(b);
        rez += ProcessString(bl_i.text(), repl_uml);
    }
    rez+="</p>\n";

    return rez;
}

//-----------------------------------------------------------------------------
QString MainWindow::AddLink_1(const QString &link, bool blank, bool zpt_before)
{
    if(link.length()<=0)return "";

    QString rez("");
    if(zpt_before) rez +=", ";
    rez += "<a href=\"" +link + "\"";
    if(blank)rez += " target=\"_blank\"";
    rez += ">"+link+"</a>";
    return rez;
}

//-----------------------------------------------------------------------------
QString MainWindow::AddLink(const QString &l_name, const QStringList &lst,
                            bool blank)
{
    if(lst.count()<=0)return "";

    QString rez = "<p>";
    if(l_name.length()>0)
        rez+= "<strong>" + l_name + "</strong>";

    for(int i=0; i<lst.count(); i++)
        rez += AddLink_1(lst.at(i), blank, i!=0);

    rez += "</p>\n";
    return rez;
}
//-----------------------------------------------------------------------------
void MainWindow::CreateStringList_1(QStringList &lst,
                                    const QString &link) const
{
    if(link.length()>0)lst.append(link);
}
//-----------------------------------------------------------------------------
// Добавляет только элементы с длинной >0
void MainWindow::CreateStringList(QStringList &lst,
                                 const QString &link1,
                                 const QString &link2,
                                 const QString &link3,
                                 const QString &link4,
                                 const QString &link5) const
{
    lst.clear();
    CreateStringList_1(lst, link1);
    CreateStringList_1(lst, link2);
    CreateStringList_1(lst, link3);
    CreateStringList_1(lst, link4);
    CreateStringList_1(lst, link5);
}

//-----------------------------------------------------------------------------
QString MainWindow::AddForum_etc(QNational nation)
{
    QStringList txt_f;
    QStringList txt_ph;
    QStringList txt_v;
    CreateStringList(txt_f,  ui->edForum->text());
    CreateStringList(txt_ph, ui->edPhoto->text(), ui->edPhoto_2->text(), ui->edPhoto_3->text(), ui->edPhoto_4->text(), ui->edPhoto_5->text());
    CreateStringList(txt_v,  ui->edVideo->text(), ui->edVideo_2->text(), ui->edVideo_3->text(), ui->edVideo_4->text(), ui->edVideo_5->text());

    QString rez("");
    switch(nation){
    case RUS:
        rez += AddLink(trUtf8("Форум: "), txt_f,  true);
        rez += AddLink(trUtf8("Фото: "),  txt_ph, true);
        rez += AddLink(trUtf8("Видео: "), txt_v,  true);
        break;
    case FIN:
        rez += AddLink("Foorumi: ",   txt_f,  true); // "Форум" = Foorumi.
        rez += AddLink("Valokuvia: ", txt_ph, true); // "Фото" = Valokuvia (kogda dva ili bolshe). Valokuva (odna)
        rez += AddLink(((txt_v.count()>1)? "Videoita: ": "Video: "),
                       txt_v,  true); // "Видео" = Videoita (kogda dva ili bolshe), Video (odin)
        break;
    case SWE:
    case ENG:
    default:
        rez += AddLink("Forum: ",  txt_f,  true);
        rez += AddLink("Photos: ", txt_ph, true);
        rez += AddLink("Video: ",  txt_v,  true);
        break;
    }
    return rez+"\n";
}
//-----------------------------------------------------------------------------
QString MainWindow::AddImgForKK()
{
    QString rez = "";
    QString str1 = "<p style=\"text-align: center;\"><img src=\"";
    QString str2 = "\" /></p>\n";
    for(int i=1; i<=ui->edPhotosCnt->value(); i++)
    {
        rez +=str1+AddImgForKK_OneImg(i)+str2;
    }
    return rez;
}
//-----------------------------------------------------------------------------
QString MainWindow::AddImgForUcoz_OneImg(int imgNum)
{
    return QFileBase::ConcatDirFile(GetPhotoPathForUcoz(), GeneratePhotoFileName(imgNum));
}
//-----------------------------------------------------------------------------
QString MainWindow::AddImgForUcoz()
{
    QString rez = "<p style=\"text-align: center;\">\n";
    QString str1 = "<img style=\"padding:5px; border:0px;\" src=\"";
    QString str2 = "\" /><br />\n";
    for(int i=1; i<=ui->edPhotosCnt->value(); i++)
    {
        rez +=str1+AddImgForUcoz_OneImg(i)+str2;
    }
    rez+="</p>\n";
    return rez;
}

//-----------------------------------------------------------------------------
QString MainWindow::GeneratePhotoFileName(int imgNum)
{
    return ui->edID->text()+"-"+ ReplaceUml_file(ui->edNameFin->text())+ "_00" +
           QString("%1").arg(imgNum) + ".jpg";
}

//-----------------------------------------------------------------------------
// Фозвращает имя каталога фоток (без имени файла) для сайта ...
QString MainWindow::GetPhotoPathFor(const QString& root_str)
{
    QString sex_str("");
    switch(ui->edMaleFemale->currentIndex()){
        case 0: sex_str = "females/";
            break;
        case 1: sex_str = "males/";
            break;
    }
    QString sub_fold = ui->edFoldNum->text();

    QString rez_str = QFileBase::ConcatDirFile(root_str, sex_str);
    rez_str         = QFileBase::ConcatDirFile(rez_str,  sub_fold);
    return rez_str;
}
//-----------------------------------------------------------------------------
// Фозвращает имя каталога фоток (без имени файла) для сайта narod.ru
QString MainWindow::GetPhotoPathForNarodRu()
{
    QString root_str("");
    if(ui->rd_ImgPathNarod->isChecked())
        root_str="../../img/";
    else
        root_str = QFileBase::ConcatDirFile(C_UCOZ_LINK, ui->ed_UcozRootFold->text());

    return GetPhotoPathFor(root_str);
}
//-----------------------------------------------------------------------------
// Фозвращает имя каталога фоток (без имени файла) для сайта ucoz.ru
QString MainWindow::GetPhotoPathForUcoz()
{
    QString root_str("");
    if(ui->rd_ImgPathNarod->isChecked())
        root_str = QFileBase::ConcatDirFile(C_NAROD_LINK, "img");
    else
        root_str = ui->ed_UcozRootFold->text();

    return GetPhotoPathFor(root_str);
}

//-----------------------------------------------------------------------------
// Фозвращает имя каталога фоток (без имени файла) для сайта narod.ru
QString MainWindow::GetPhotoPathForKK()
{
    QString root_str("");
    if(ui->rd_ImgPathNarod->isChecked())
        root_str = QFileBase::ConcatDirFile(C_NAROD_LINK, "img");
    else
        root_str = QFileBase::ConcatDirFile(C_UCOZ_LINK, ui->ed_UcozRootFold->text());

    return GetPhotoPathFor(root_str);
}



//-----------------------------------------------------------------------------
// Фотки для narod.ru
QString MainWindow::AddImgForNarodRu()
{
    QString rez("");
    QString str_start = "<img class=\"img_style\" src=\"";
    QString str_root = GetPhotoPathForNarodRu();
    QString str_end = "\" border=\"0\" />\n";
    for(int i=1; i<=ui->edPhotosCnt->value(); i++)
    {
        rez += str_start +
               QFileBase::ConcatDirFile(str_root,GeneratePhotoFileName(i))+
               str_end;
    }

    return rez;
}
//-----------------------------------------------------------------------------
void MainWindow::GenerateRezPath()
{
    QString str_name = GeneratePhotoFileName(1);
    ui->ed_ResultPhotoPathNarod->setText(QFileBase::ConcatDirFile(GetPhotoPathForNarodRu(), str_name));
    ui->ed_ResultPhotoPathUcoz->setText(QFileBase::ConcatDirFile(GetPhotoPathForUcoz(), str_name));
    ui->ed_ResultPhotoPathKK->setText(QFileBase::ConcatDirFile(GetPhotoPathForKK(), str_name));
}


//-----------------------------------------------------------------------------

QString MainWindow::AddForum_podp()
{
    QString rez("");
    rez+= "<p> </p>\n";

    rez+= "<div class=\"contakt\">";
    rez+= "T: +7-(950)-035-57-89\n";
    rez+= "<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;Stray-petersburg&#64;yandex&#46;ru\">Stray-petersburg&#64;yandex&#46;ru</a></div>\n\n";

    rez+= "<hr size=\"1\" class=\"hrclass\"/>\n";
    return rez;
}
//-----------------------------------------------------------------------------
QString MainWindow::AddForum_podp_Ucoz(QNational nation)
{
    QString titl("");
    QString anch("");

    switch(nation){
        case RUS:{
            titl = trUtf8("Контакты");
            anch = "";
            break;
        }
        case FIN:{
            titl = trUtf8("Yhteystiedot");
            anch = "fin";
            break;
        }
        case ENG:{
            titl = trUtf8("Contacts");
            anch = "eng";
            break;
        }
        case SWE:{
            titl = trUtf8("Contacts SUI");
            anch = "sui";
            break;
        }
    }

    return "<p><a href=\"/index/0-3#" + anch + "\"><b>" + titl+ "</b></a></p>\n";
}
//-----------------------------------------------------------------------------
QString MainWindow::CreateNarodText(QNational nation)
{
    bool is_rus = (nation==RUS);

    QString rez_str = CreateNarodCapt(nation);

    QString dop_links = CreateDopLinks(nation, true);

    // доп.ссылки до текста
    if(ui->rb_before_text->isChecked())
        rez_str  += dop_links;

    // текст
    rez_str += ProcessText(((is_rus)? ui->edRusIn : ui->edFinIn), true, true);

    // доп.ссылки после текста
    if(ui->rb_after_text->isChecked())
        rez_str  += dop_links;

    // ссылки на форум, фото, видео
    rez_str += AddForum_etc(nation);

    // фотки
    rez_str += AddImgForNarodRu();

    // подпись
    rez_str += AddForum_podp();

    return rez_str;
}

//-----------------------------------------------------------------------------
QString MainWindow::CreateKKText()
{
    QString rez_str = CreateKKCapt();

    QString dop_links = CreateDopLinks(FIN, true);

    // доп.ссылки до текста
    if(ui->rb_before_text->isChecked())
        rez_str  += dop_links;

    // текст
    rez_str += ProcessText(ui->edFinIn, false, true);

    // доп.ссылки после текста
    if(ui->rb_after_text->isChecked())
        rez_str  += dop_links;

    // ссылки на форум, фото, видео
    rez_str += AddForum_etc(FIN);

    // фотки
    rez_str += AddImgForKK();

    return rez_str;
}

//-----------------------------------------------------------------------------
QString MainWindow::AddUcozFlag(QNational nation)
{
    QString title  = "";
    QString fl_lnk = "";


    switch(nation){
    case RUS:{
        title  = "RUS";
        fl_lnk = "rus";
        break;
        }
    case FIN:{
        title  = "FIN";
        fl_lnk = "fin";
        break;
        }
    case SWE:{
        title  = "SWE";
        fl_lnk = "swe";
        break;
        }
    case ENG:
    default:{
        title  = "FIN";
        fl_lnk = "eng";
        break;
        }

    }
    QString rez_str = "<p style=\"text-align:justify;\">";
    rez_str += "<img style=\"padding:5px;\" src=\"/flags/"+fl_lnk+".png\" border=\"0\" alt=\""+ title +
            "\" title=\"" + title +"\" />";
    return rez_str;
}

//-----------------------------------------------------------------------------
// Текст для Ucoz
QString MainWindow::CreateUcozTex(QNational nation)
{
    bool first_p = false;

    QString rez_str = "";

    rez_str += AddUcozFlag(nation);
    QString dop_links = CreateDopLinks(nation, first_p);

    // доп.ссылки до текста
    if(ui->rb_before_text->isChecked()){
        rez_str  += dop_links;
        if(dop_links.length()>0)
            first_p = true;
    }

    // текст
    QPlainTextEdit* edit1=NULL;
    switch(nation){
        case ENG:
        case RUS:edit1=ui->edRusIn; break;
        case FIN:edit1=ui->edFinIn; break;
        case SWE:edit1=ui->edSweIn; break;
    }

    rez_str += ProcessText(edit1, false, first_p);

    // доп.ссылки после текста
    if(ui->rb_after_text->isChecked())
        rez_str  += dop_links;

    // ссылки на форум, фото, видео
    rez_str += AddForum_etc(nation);

    return rez_str;
}

//-----------------------------------------------------------------------------
// Аватар для Ucoz
QString MainWindow::CreateUcozTextShort()
{
    QString sz_str = "";

    switch(ui->cb_Avatar->currentIndex()){
        case 0:// широкий
            sz_str = "width=\"150\"";
            break;
        case 1:// высокий
            sz_str = "height=\"150\"";
            break;
    }

    QString rez = "<p style=\"text-align: left;\">\n";
    QString str1 = "<img src=\"";
    QString str2 = "\" "+ sz_str+ " border=\"0\" />\n";
    rez += str1 + AddImgForUcoz_OneImg(1) + str2;
    rez += "</p>\n";
    return rez;
}

//-----------------------------------------------------------------------------
// Текст на русском или финском для Ucoz
QString MainWindow::CreateUcozTextRusFin()
{
    QString rez_str = CreateUcozTex(RUS);
    rez_str += AddForum_podp_Ucoz(RUS);// подпись

    rez_str += CreateUcozTex(FIN);
    rez_str += AddForum_podp_Ucoz(FIN);// подпись

    // фотки
    rez_str += AddImgForUcoz();

    return rez_str;
}

//-----------------------------------------------------------------------------
// Текст на шведском для Ucoz
QString MainWindow::CreateUcozTextSwe()
{
    QString rez_str = CreateUcozTex(SWE);
    rez_str += AddForum_podp_Ucoz(SWE);// подпись

    // фотки
    rez_str += AddImgForUcoz();

    return rez_str;
}
//-----------------------------------------------------------------------------
void MainWindow::CreateUcozPesName()
{
    QString rez = ui->edID->text()+". ";

    QString rez_rus_fin = rez + ui->edNameRus->text() + " - " + ui->edNameFin->text();
    ui->ed_UcozName->setText(rez_rus_fin);

    QString swe_name = ui->edNameSwe->text();
    if(swe_name.length()>0){
        QString rez_swe = rez + swe_name;
        ui->ed_UcozName_swe->setText(rez_swe);
    }else{
        ui->ed_UcozName_swe->setText(rez_rus_fin);
    }
}


//-----------------------------------------------------------------------------
void MainWindow::CreateUcozD_R_V()
{
    ui->ed_Ucoz_Date->setText(ui->edDateRojd->text());
    ui->ed_Ucoz_Rost->setText(ui->edRost->text());
    ui->ed_Ucoz_Ves->setText (ui->edVes->text());
}

//-----------------------------------------------------------------------------
// Генерирует выходной текст для полей: папка с фото, имя файлов с фото
void MainWindow::GenerateUcozFileNamesAndDirTxt()
{
    ui->ed_Ucoz_ImgFold->setText(GetPhotoPathForUcoz());
    ui->ed_Ucoz_Img1->setText(GeneratePhotoFileName(1));
    ui->ed_Ucoz_ImgCnt->setText(GeneratePhotoFileName(ui->edPhotosCnt->value()));
}

//-----------------------------------------------------------------------------
// Запуск
void MainWindow::on_pushButton_clicked()
{
    ui->edRusOut->document()->setPlainText(CreateNarodText(RUS));
    ui->edFinOut->document()->setPlainText(CreateNarodText(FIN));
    ui->edFinOut_KK->document()->setPlainText(CreateKKText());

    CreateUcozPesName();
    CreateUcozD_R_V();

    ui->edFinOutName_KK->setText(ui->edNameFin->text());

    ui->edFinOut_Ucoz_Short->document()->setPlainText(CreateUcozTextShort());
    ui->edFinOut_Ucoz->document()->setPlainText(CreateUcozTextRusFin());
    ui->edSweOut_Ucoz->document()->setPlainText(CreateUcozTextSwe());

    GenerateUcozFileNamesAndDirTxt();
}
//-----------------------------------------------------------------------------
void MainWindow::on_edMaleFemale_currentIndexChanged(int index)
{
    QVariant var=0;
    switch(index){
        case 0:cfg_file->GetParam("FemaleFold", var);
            break;
        case 1:cfg_file->GetParam("MaleFold", var);
            break;
    }
    if(var.toInt()==0)ui->edFoldNum->setText("");
    else              ui->edFoldNum->setText(var.toString());

    GenerateRezPath();
}
//-----------------------------------------------------------------------------
void MainWindow::on_edFoldNum_editingFinished()
{
    switch(ui->edMaleFemale->currentIndex()){
        case 0:cfg_file->SetParam("FemaleFold", ui->edFoldNum->text());
            break;
        case 1:cfg_file->SetParam("MaleFold", ui->edFoldNum->text());
            break;
    }
}
//-----------------------------------------------------------------------------
void MainWindow::on_cb_m_rus_currentIndexChanged(int index)
{
    ui->cb_m_fin->setCurrentIndex(index);
    ui->cb_m_eng->setCurrentIndex(index);
}
//-----------------------------------------------------------------------------
void MainWindow::on_cb_m_fin_currentIndexChanged(int index)
{
    ui->cb_m_rus->setCurrentIndex(index);
    ui->cb_m_eng->setCurrentIndex(index);
}
//-----------------------------------------------------------------------------
void MainWindow::on_cb_m_eng_currentIndexChanged(int index)
{
    ui->cb_m_rus->setCurrentIndex(index);
    ui->cb_m_fin->setCurrentIndex(index);
}
//-----------------------------------------------------------------------------
void MainWindow::on_btn_copy_rus_clicked()
{
    QApplication::clipboard()->setText(ui->cb_m_rus->currentText());
}
//-----------------------------------------------------------------------------
void MainWindow::on_btn_copy_fin_clicked()
{
    QApplication::clipboard()->setText(ui->cb_m_fin->currentText());
}
//-----------------------------------------------------------------------------
void MainWindow::on_btn_copy_eng_clicked()
{
    QApplication::clipboard()->setText(ui->cb_m_eng->currentText());
}
//-----------------------------------------------------------------------------
void MainWindow::ClearDopLinks()
{
    ui->doplnk_text_begin_r->setText("");
    ui->doplnk_text_begin_f->setText("");

    ui->doplnk_1->setText("");
    ui->doplnk_1_r->setText("");
    ui->doplnk_1_f->setText("");

    ui->doplnk_2->setText("");
    ui->doplnk_2_r->setText("");
    ui->doplnk_2_f->setText("");

    ui->doplnk_3->setText("");
    ui->doplnk_3_r->setText("");
    ui->doplnk_3_f->setText("");

    ui->doplnk_4->setText("");
    ui->doplnk_4_r->setText("");
    ui->doplnk_4_f->setText("");

    ui->doplnk_5->setText("");
    ui->doplnk_5_r->setText("");
    ui->doplnk_5_f->setText("");

    ui->doplnk_text_end_r->setText("");
    ui->doplnk_text_end_f->setText("");
}

//-----------------------------------------------------------------------------
void MainWindow::on_btn_ClearDopLinks_clicked()
{
    QMessageBox msgBox(QMessageBox::Question, windowTitle(),
                       trUtf8("Очистить все дополнительные ссылки?"),
                       QMessageBox::Yes | QMessageBox::No);
    msgBox.setDefaultButton(QMessageBox::No);
    if(msgBox.exec()!=QMessageBox::Yes) return;

    ClearDopLinks();
}
//-----------------------------------------------------------------------------
void MainWindow::ClearAllData()
{
    ui->edNameRus->setText("");
    ui->edNameFin->setText("");
    ui->edNameSwe->setText("");

    ui->edDateRojd->setText("");

    ui->edRost->setText("");
    ui->edVes->setText("");

    ui->edForum->setText("");

    ui->edPhoto->setText("");
    ui->edPhoto_2->setText("");
    ui->edPhoto_3->setText("");
    ui->edPhoto_4->setText("");
    ui->edPhoto_5->setText("");

    ui->edVideo->setText("");
    ui->edVideo_2->setText("");
    ui->edVideo_3->setText("");
    ui->edVideo_4->setText("");
    ui->edVideo_5->setText("");

    ui->edRusIn->setPlainText("");
    ui->edFinIn->setPlainText("");
    ui->edSweIn->setPlainText("");

    ClearDopLinks();
}

//-----------------------------------------------------------------------------

void MainWindow::on_btn_ClearAll_clicked()
{
    QMessageBox msgBox(QMessageBox::Question, windowTitle(),
                       trUtf8("Очистить все входные данные в формах?"),
                       QMessageBox::Yes | QMessageBox::No);
    msgBox.setDefaultButton(QMessageBox::No);
    if(msgBox.exec()!=QMessageBox::Yes) return;
    ClearAllData();
}


void MainWindow::on_rd_ImgPathNarod_toggled(bool checked)
{
    if(checked)
        cfg_file->SetParam("RbImgFolder", "narod");
    else
        cfg_file->SetParam("RbImgFolder", "ucoz");

    GenerateRezPath();
}

void MainWindow::on_rd_ImgPathUcoz_toggled(bool checked)
{
    if(checked)
        cfg_file->SetParam("RbImgFolder", "ucoz");
    else
        cfg_file->SetParam("RbImgFolder", "narod");

    GenerateRezPath();
}


void MainWindow::on_ed_UcozRootFold_textEdited(QString text)
{
    cfg_file->SetParam("UcozPhotoPath", text);

    GenerateRezPath();
}

void MainWindow::on_edNameFin_textEdited(QString )
{
    GenerateRezPath();
}

void MainWindow::on_edFoldNum_textEdited(QString )
{
    GenerateRezPath();
}

void MainWindow::on_btnu_cpy_NRus_clicked()
{
    QApplication::clipboard()->setText(ui->ed_UcozName->text());
}

void MainWindow::on_btnu_cpy_NSwe_clicked()
{
    QApplication::clipboard()->setText(ui->ed_UcozName_swe->text());
}

void MainWindow::on_btnu_cpy_DR_clicked()
{
    QApplication::clipboard()->setText(ui->ed_Ucoz_Date->text());
}

void MainWindow::on_btnu_cpy_Rost_clicked()
{
    QApplication::clipboard()->setText(ui->ed_Ucoz_Rost->text());
}

void MainWindow::on_btnu_cpy_Ves_clicked()
{
    QApplication::clipboard()->setText(ui->ed_Ucoz_Ves->text());
}

void MainWindow::on_btnu_cpy_ImgFolder_clicked()
{
    QApplication::clipboard()->setText(ui->ed_Ucoz_ImgFold->text());
}

void MainWindow::on_btnu_cpy_Img1_clicked()
{
    QApplication::clipboard()->setText(ui->ed_Ucoz_Img1->text());
}

void MainWindow::on_btnu_cpy_ImgCnt_clicked()
{
    QApplication::clipboard()->setText(ui->ed_Ucoz_ImgCnt->text());
}

void MainWindow::on_btnu_cpy_Avatar_clicked()
{
    QApplication::clipboard()->setText(ui->edFinOut_Ucoz_Short->document()->toPlainText());
}

void MainWindow::on_btnu_cpy_RusText_clicked()
{
    QApplication::clipboard()->setText(ui->edFinOut_Ucoz->document()->toPlainText());
}

void MainWindow::on_btnu_cpy_SweText_clicked()
{
    QApplication::clipboard()->setText(ui->edSweOut_Ucoz->document()->toPlainText());
}

void MainWindow::on_btnKK_cpy_Name_clicked()
{
    QApplication::clipboard()->setText(ui->edFinOutName_KK->text());
}

void MainWindow::on_btnKK_cpy_Text_clicked()
{
    QApplication::clipboard()->setText(ui->edFinOut_KK->document()->toPlainText());
}

void MainWindow::on_btn_CpySh_Varatut_clicked()
{
    QApplication::clipboard()->setText(ui->lineEdit_5->text());
}

void MainWindow::on_btn_CpySh_Vybrana_clicked()
{
    QApplication::clipboard()->setText(ui->lineEdit_6->text());
}

void MainWindow::on_btn_CpySh_Vybran_clicked()
{
    QApplication::clipboard()->setText(ui->lineEdit_7->text());
}

void MainWindow::on_btn_CpySh_NewPhotos_clicked()
{
    QApplication::clipboard()->setText(ui->lineEdit_8->text());
}

void MainWindow::on_btn_CpySh_OldPhotos_clicked()
{
    QApplication::clipboard()->setText(ui->lineEdit_9->text());
}

void MainWindow::on_btn_CpySh_NewPhotos_F_clicked()
{
    QApplication::clipboard()->setText(ui->lineEdit_12->text());
}

void MainWindow::on_btn_CpySh_OldPhotos_F_clicked()
{
    QApplication::clipboard()->setText(ui->lineEdit_13->text());
}

void MainWindow::on_btn_CpySh_NewPhotos_R_clicked()
{
    QApplication::clipboard()->setText(ui->lineEdit_10->text());
}

void MainWindow::on_btn_CpySh_OldPhotos_R_clicked()
{
    QApplication::clipboard()->setText(ui->lineEdit_11->text());
}

void MainWindow::on_btn_CpySh_HappyFin_clicked()
{
    QApplication::clipboard()->setText(ui->lineEdit_HappyFin->text());
}

void MainWindow::on_btn_CpySh_HappyRus_clicked()
{
    QApplication::clipboard()->setText(ui->lineEdit_HappyRus->text());
}

void MainWindow::on_btn_CpySh_Pietari_clicked()
{
    QApplication::clipboard()->setText(ui->lineEdit_3->text());
}
